package net.song.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * a.这里不是走 HTTP + JSON 模式，使用了 @Controller 而不是先前的 @RestController
 * 
 * b.方法返回值是 String 类型，和 application.properties 配置的 Freemarker 文件配置路径下的各个 *.ftl 文件名一致。这样才会准确地把数据渲染到 ftl 文件里面进行展示。
 * 
 * c.用 Model 类，向 Model 加入数据，并指定在该数据在 Freemarker 取值指定的名称。
 * 
 * @description :
 * @date : 2018-09-07 17:32
 * @author : leoSong
 *
 */
@Controller
public class DocController {

	@RequestMapping(value = "/hello")
	@ResponseBody
	public String hello(@RequestParam(value = "name", required = false) String name) {
		String result = "hello--+-sq  " + name;
		System.out.println(result);
		return result;
	}

	@RequestMapping("index")
	public String helloFtl(Map<String, Object> map) {
		map.put("hello", "Hello FreeMarker");
		return "index";
	}

	@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
	public String findOneCity(Model model, @PathVariable("id") Long id) {
		model.addAttribute("city", "id:" + id);
		return "city";
	}

	@RequestMapping(value = "/api/city", method = RequestMethod.GET)
	public String findAllCity(Model model) {
		List<String> cityList = new ArrayList<>();
		cityList.add("Shenzhen");
		cityList.add("Shangdong");
		model.addAttribute("cityList", cityList);
		return "cityList";
	}

	public static void main(String[] args) {
		Map<String, Object> hashMap = new HashMap<>();
		hashMap.put("dsdd", 123);
	}
}
